<template>
  <div class="cartcontrol">
    <div class="decrease" v-show="food.count>0" @click.stop="decCart">
      <transition name="move">
        <span class="inner icon-remove_circle_outline"></span>
      </transition>
    </div>
    <div class="count" v-show="food.count>0">{{food.count}}</div>
    <div class="add icon-add_circle" @click.stop="addCart"></div>
  </div>
</template>

<script>
import Vue from "vue";
export default {
  name: "cartControl",
  props: {
    food: {
      type: Object
    }
  },
  methods: {
    addCart() {
      if (!this.food.count) {
        Vue.set(this.food, "count", 0);
        //需要调用一个API，才能被观测到
        //this.food.count = 1; 添加不存在的字段的时候 不能直接这样赋值
      }
      this.food.count++;
    },
    decCart() {
      if (this.food.count > 0) {
        this.food.count--;
      }
    }
  }
};
</script>

<style lang="stylus" scoped>
.cartcontrol
  font-size: 0
  .decrease
    display: inline-block
    padding: 6px
    &.move-enter-active, &.move-leave-active
      transition: opacity 20s
    &.move-enter, &.move-leave-to
      opacity: 0
    .inner
      display: inline-block
      line-height: 24px
      font-size: 24px
      color: rgb(0, 160, 220)
  .count
    display: inline-block
    vertical-align: top
    width: 12px
    padding-top: 6px
    line-height: 24px
    text-align: center
    font-size: 10px
    color: rgb(147, 153, 159)
  .add
    display: inline-block
    line-height: 24px
    padding: 6px
    font-size: 24px
    color: rgb(0, 160, 220)
</style>